package com.hnzbxy.api.biz.controller;

import com.hnzbxy.api.biz.converter.DepartmentConverter;
import com.hnzbxy.api.biz.entity.Department;
import com.hnzbxy.api.biz.service.DepartmentService;
import com.hnzbxy.api.biz.vo.DepartmentEditVO;
import com.hnzbxy.api.biz.vo.DepartmentVO;
import com.hnzbxy.api.biz.vo.DeptNodeVO;
import com.hnzbxy.api.biz.vo.DeptTransferItemVO;
import com.hnzbxy.api.common.annotation.ControllerEndpoint;
import com.hnzbxy.api.common.bean.ResponseBean;
import com.hnzbxy.api.system.vo.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门管理控制器
 *
 * @Author xiaoge
 * @Date 2020/3/7 16:24
 * @Version 1.0
 **/
@RestController
@RequestMapping("/department")
@Validated
@Api(value = "系统部门模块", tags = "系统部门接口")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 部门列表
     *
     * @return 分页部门列表
     */
    @ApiOperation(value = "部门列表", notes = "模糊查询部门列表，支持分页")
    @GetMapping("/findDeptList")
    public ResponseBean findDeptList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     DepartmentVO departmentVO) {
        PageVO<DepartmentVO> deptList = departmentService.findDeptList(pageNum, pageSize, departmentVO);
        return ResponseBean.success(deptList);
    }

    /**
     * 部门详情
     *
     * @param id 部门ID
     * @return 部门详情信息
     */
    @ApiOperation(value = "部门详情", notes = "根据部门ID获取部门详细信息")
    @GetMapping("/info/{id}")
    public ResponseBean info(@PathVariable Long id) {
        DepartmentVO departmentVO = departmentService.info(id);
        return ResponseBean.success(departmentVO);
    }

    /**
     * 加载部门树
     *
     * @return 部门树形结构
     */
    @ApiOperation(value = "加载部门树", notes = "获取部门树形结构，用于部门选择等场景")
    @GetMapping("/findDeptTree")
    public ResponseBean findDeptTree() {
        List<DeptNodeVO> deptTreeVOS = departmentService.findDeptTree();
        return ResponseBean.success(deptTreeVOS);
    }

    /**
     * 分配上级部门
     *
     * @param id           部门ID
     * @param parentDeptId 上级部门ID
     * @return 操作结果
     */
    @ControllerEndpoint(exceptionMessage = "分配上级部门失败", operation = "分配上级部门")
    @ApiOperation(value = "分配上级部门", notes = "为部门设置上级部门")
    @RequiresPermissions({"department:assignParent"})
    @PostMapping("/{id}/assignParent")
    public ResponseBean assignParent(@PathVariable Long id, @RequestParam Long parentDeptId) {
        departmentService.assignParent(id, parentDeptId);
        return ResponseBean.success();
    }

    /**
     * 删除部门
     *
     * @param id 部门ID
     * @return 操作结果
     */
    @ControllerEndpoint(exceptionMessage = "删除部门失败", operation = "删除部门")
    @RequiresPermissions({"department:delete"})
    @ApiOperation(value = "删除部门", notes = "根据部门ID删除部门信息")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        System.out.println(id);
        departmentService.deleteById(id);
        return ResponseBean.success();
    }

    /**
     * 更新部门状态
     *
     * @param id     部门ID
     * @param status 部门状态：0-未启用，1-正常，2-暂停，3-撤销
     * @return 操作结果
     */
    @ControllerEndpoint(exceptionMessage = "更新部门状态失败", operation = "更新部门状态")
    @ApiOperation(value = "更新部门状态", notes = "修改部门状态（0-未启用，1-正常，2-暂停，3-撤销）")
    @RequiresPermissions({"department:status"})
    @PutMapping("/updateStatus/{id}/{status}")
    public ResponseBean updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        departmentService.updateStatus(id, status);
        return ResponseBean.success();
    }

    /**
     * 更新部门
     *
     * @param id            部门ID
     * @param departmentEditVO 部门编辑信息
     * @return 操作结果
     */
    @ControllerEndpoint(exceptionMessage = "更新部门失败", operation = "更新部门")
    @ApiOperation(value = "更新部门", notes = "更新部门信息")
    @RequiresPermissions({"department:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id, @RequestBody @Validated DepartmentEditVO departmentEditVO) {
        departmentService.update(id, departmentEditVO);
        return ResponseBean.success();
    }

    /**
     * 编辑部门
     *
     * @param id 部门ID
     * @return 部门编辑信息
     */
    @ApiOperation(value = "编辑部门", notes = "获取部门详情，用于编辑")
    @RequiresPermissions({"department:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        DepartmentEditVO departmentEditVO = departmentService.edit(id);
        return ResponseBean.success(departmentEditVO);
    }

    /**
     * 添加部门
     *
     * @param departmentVO 部门信息
     * @return 操作结果
     */
    @ControllerEndpoint(exceptionMessage = "添加部门失败", operation = "添加部门")
    @ApiOperation(value = "添加部门", notes = "添加新部门信息")
    @RequiresPermissions({"department:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated DepartmentVO departmentVO) {
        departmentService.add(departmentVO);
        return ResponseBean.success();
    }

    /**
     * 部门选择列表
     *
     * @param excludeId 排除的部门ID（用于选择上级部门时排除自身）
     * @return 部门选择项列表
     */
    @ApiOperation(value = "部门选择列表", notes = "获取部门列表，用于部门选择场景")
    @GetMapping("/selectList")
    public ResponseBean selectList(@RequestParam(required = false) Long excludeId) {
        List<Department> list = departmentService.findAll(excludeId);
        List<DeptTransferItemVO> items = DepartmentConverter.converterToDeptTransferItem(list);
        return ResponseBean.success(items);
    }

    /**
     * 导出部门Excel
     *
     * @param response 响应对象
     */
    @ApiOperation(value = "导出部门Excel", notes = "导出所有部门信息到Excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("department:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败", operation = "导出部门excel")
    public void export(HttpServletResponse response) {
        List<Department> departments = this.departmentService.findAll();
        ExcelKit.$Export(Department.class, response).downXlsx(departments, false);
    }
}
    